OpenFeign একটি ডিক্লারেটিভ HTTP ক্লায়েন্ট, যা স্প্রিং বুট অ্যাপ্লিকেশনে API কলের প্রক্রিয়াকে সহজ এবং আরও সংগঠিত করে। এটি Spring Cloud-এর অংশ এবং মাইক্রোসার্ভিসের মধ্যে যোগাযোগের জন্য বিশেষভাবে কার্যকর।
Maven:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
Gradle:
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
@EnableFeignClients
এনোটেশন ব্যবহার করে OpenFeign সক্রিয় করতে হবে।
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class FeignClientApplication {
public static void main(String[] args) {
SpringApplication.run(FeignClientApplication.class, args);
}
}
একটি ইন্টারফেস তৈরি করে API এর মেথড ডিফাইন করুন। এটি OpenFeign ব্যবহার করে HTTP রিকোয়েস্ট পাঠাবে।
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
// API বেস URL প্রদান করুন
@FeignClient(name = "userClient", url = "https://api.example.com")
public interface UserClient {
// GET রিকোয়েস্ট
@GetMapping("/users/{id}")
String getUserById(@PathVariable("id") Long id);
}
ক্লায়েন্টটি সার্ভিস ক্লাসে Inject করে ব্যবহার করুন।
import org.springframework.stereotype.Service;
@Service
public class UserService {
private final UserClient userClient;
public UserService(UserClient userClient) {
this.userClient = userClient;
}
public String getUserDetails(Long id) {
return userClient.getUserById(id);
}
}
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignConfig {
@Bean
public Logger.Level feignLoggerLevel() {
return Logger.Level.FULL; // FULL logging level
}
}
ইন্টারফেসে @FeignClient
এর মধ্যে কনফিগারেশন যুক্ত করুন:
@FeignClient(name = "userClient", url = "https://api.example.com", configuration = FeignConfig.class)
public interface UserClient {
@GetMapping("/users/{id}")
String getUserById(@PathVariable("id") Long id);
}
application.properties
বা application.yml
-এ টাইমআউট নির্ধারণ করুন:
feign.client.config.default.connectTimeout=5000
feign.client.config.default.readTimeout=5000
@GetMapping("/users/{id}")
String getUserById(@PathVariable("id") Long id);
@PostMapping("/users")
String createUser(@RequestBody User user);
@PutMapping("/users/{id}")
String updateUser(@PathVariable("id") Long id, @RequestBody User user);
@DeleteMapping("/users/{id}")
void deleteUser(@PathVariable("id") Long id);
@FeignClient
এ url
এর পরিবর্তে name
ব্যবহার করুন এবং application.yml
এ সেবা নিবন্ধন করুন:
@FeignClient(name = "user-service")
public interface UserClient {
@GetMapping("/users/{id}")
String getUserById(@PathVariable("id") Long id);
}
application.yml
:
user-service:
ribbon:
listOfServers: http://localhost:8081, http://localhost:8082
OpenFeign এর জন্য কাস্টম ErrorDecoder ব্যবহার করতে পারেন।
import feign.Response;
import feign.codec.ErrorDecoder;
public class CustomErrorDecoder implements ErrorDecoder {
@Override
public Exception decode(String methodKey, Response response) {
if (response.status() == 404) {
return new RuntimeException("Resource not found!");
}
return new Exception("Generic error");
}
}
@FeignClient(name = "userClient", url = "https://api.example.com", configuration = FeignConfig.class)
public interface UserClient {}
FeignConfig
:@Configuration
public class FeignConfig {
@Bean
public ErrorDecoder errorDecoder() {
return new CustomErrorDecoder();
}
}
OpenFeign ব্যবহার করে স্প্রিং বুট ক্লায়েন্ট তৈরি করা একটি সহজ এবং কার্যকর পদ্ধতি, বিশেষ করে মাইক্রোসার্ভিস আর্কিটেকচারের জন্য। এটি HTTP রিকোয়েস্ট হ্যান্ডলিং এবং মাইক্রোসার্ভিসের মধ্যে যোগাযোগের জন্য উন্নত অভিজ্ঞতা প্রদান করে।
Read more